package com.springmvc.controller;

import com.springmvc.constant.QueryConstant;
import com.springmvc.entity.leibie;
import com.springmvc.entity.wenzhang;
import com.springmvc.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    @RequestMapping("list")
    public String list(HttpServletRequest request) {
        return "article/articleList";
    }

    /**
     * 分页获取文章列表
     *
     * @param request
     * @param wenzhang
     * @return
     */
    @ResponseBody
    @RequestMapping("articleData")
    public Map<String, Object> articleData(HttpServletRequest request, wenzhang wenzhang) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<wenzhang> list = articleService.getListByPage(wenzhang);
            Long count = articleService.getCount(wenzhang);
            map.put("code", QueryConstant.TABLE_SUCCESS_CODE);
            map.put("msg", QueryConstant.SUCCESS_MSG);
            map.put("data", list);
            map.put("count", count);
        } catch (Exception e) {
            map.put("code", QueryConstant.TABLE_FAIL_CODE);
            map.put("msg", e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("form")
    public String from(HttpServletRequest request, Model model) {
        List<leibie> list = articleService.getArticleGroup();
        model.addAttribute("groupList", list);
        return "article/articleForm";
    }

    @RequestMapping("update")
    public String update(HttpServletRequest request, Model model, wenzhang wenzhang) {
        wenzhang = articleService.getByPrimaryId(wenzhang.getId());
        List<leibie> list = articleService.getArticleGroup();
        model.addAttribute("wenzhang", wenzhang);
        model.addAttribute("groupList", list);
        return "article/updateForm";
    }

    @ResponseBody
    @RequestMapping("save")
    public Map<String, Object> save(HttpServletRequest request, wenzhang wenzhang) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            //TODO: 要添加文章创建人和修改人
            String user = String.valueOf(request.getSession().getAttribute("LOGIN_USER"));
            if (wenzhang.getId() == null) {
                articleService.insert(wenzhang);
            } else {
                articleService.update(wenzhang);
            }
            map.put("code", QueryConstant.TABLE_SUCCESS_CODE);
            map.put("msg", QueryConstant.SUCCESS_MSG);
        } catch (Exception e) {
            map.put("code", QueryConstant.TABLE_FAIL_CODE);
            map.put("msg", e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("alter")
    public Map<String, Object> alter(HttpServletRequest request, wenzhang wenzhang) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String user = String.valueOf(request.getSession().getAttribute("LOGIN_USER"));
            articleService.update(wenzhang);
            map.put("code", QueryConstant.TABLE_SUCCESS_CODE);
            map.put("msg", QueryConstant.SUCCESS_MSG);
        } catch (Exception e) {
            map.put("code", QueryConstant.TABLE_FAIL_CODE);
            map.put("msg", e.getMessage());
            e.printStackTrace();
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("delete")
    public Map<String, Object> delete(HttpServletRequest request, Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            int dl=articleService.deleteByPrimaryKey(id);
            map.put("code", QueryConstant.TABLE_SUCCESS_CODE);
            map.put("msg", QueryConstant.SUCCESS_MSG);
            map.put("dl", dl);
        } catch (Exception e) {
            map.put("code", QueryConstant.TABLE_FAIL_CODE);
            map.put("msg", e.getMessage());
            e.printStackTrace();
        }
        return map;
    }
}
